<?
include_once "fbmain.php";
include_once "common_functions.php";
include_once "configuration.php";

ob_start();
system("/bin/date");
echo("<html><head></head><body>");


//print_r($_GET);
$put_scr = true;
////////////////////////////////////////////////
//
// get the IP from the user
//
//echo $_SERVER['REMOTE_ADDR'];  
$ip_addr = get_ip();
////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
 //if user is logged in and session is valid... retrieve all the stuff
 //////////////////////////////////////////////////////////////////////
    
    if (1){ // if($fbme)
	
	$start_time_today = date("Ymd");
	$start_time_time = date("His");

	$start_overall_duration = compute_time();

  	$starttime_fb_login_retrieve = compute_time();

    /*
    	//Retriving FRIENDS list
        try{
            $friends = $facebook->api('/me/friends');
        }
        catch(Exception $o){
            d($o);
        }
        
        //Retriving EVENTS list
        try{
            $events = $facebook->api('/me/events');
        }
        catch(Exception $o){
            d($o);
        }
    */
        //Retriving TAGS list
        try{
        	$me_tags = $facebook->api('/me/photos');
        	//echo $me_tags;
        }
        catch(Exception $o){
            d($o);
        }
          
         
     $param3  =   array(
	    'method'    => 'fql.query',
	
	'query' => 'SELECT xcoord, ycoord, text, created, subject, pid FROM photo_tag WHERE subject='.$uid.' LIMIT 30', 
	
	'callback'  => ''
	);	
	$photos_me_tagged = $facebook->api($param3);
	
	//print_r($photos_me_tagged[0]['pid']);
	//print_r($photos_me_tagged);
	
	$number_photo[0] = count($photos_me_tagged);
	

         
        $param  =   array(
	    'method'    => 'fql.query',	
	'query' => 'SELECT src_big, owner, caption, link, created, src, pid FROM photo WHERE pid IN (SELECT pid FROM photo_tag WHERE subject='.$uid.') LIMIT 30', 

	
	'callback'  => ''
	);	
	$photos_me = $facebook->api($param);
	//echo $photos_me;

	//ksort($photos_me);
	//echo $photos_me;


	$number_photo[1] = count($photos_me);

	$endtime_fb_login_retrieve = compute_time();
   
}

//////////////////////////////////////////////////
// create directory where to store the tmp and final files
//
// print_r($fbme['id']);
// if(!empty($fbme['id']))
if(1){

	$today = date("Ymd");
	$time = date("His");

	$dir_log = "/log/";

	$prefix_dir = "store/".$today."_".$time."_".$fbme['id'];
	$dir_tmp = $prefix_dir."/tmp/";
	$dir_final = $prefix_dir."/final/";
	$dir_final_log = $prefix_dir."/log/";
	$dir_check = $prefix_dir."/check/";
	$dir_video = $prefix_dir."/video/";

	mkdir($dir_tmp, 0700, true);
	mkdir($dir_final, 0700, true);
	mkdir($dir_check, 0700, true);
	mkdir($dir_video, 0700, true);
	mkdir($dir_final_log, 0700, true);	

}



if(1){

if ($put_scr==true) echo("<br>");

$myFile1 = $dir_tmp."fb_data_".$fbme['id'].".txt";
$fid1 = fopen($myFile1, 'w') or die("can't open file");

$i=0;
if ($put_scr==true) echo("<br>I am saving the images to the directory...");
for($j=0;$j<count($photos_me_tagged);$j++){

	for($k=0;$k<count($photos_me);$k++){
		if (($photos_me_tagged[$j]['pid'])===($photos_me[$k]['pid'])){
		
		$numpad=number_pad($i,4);
		$counter[$i] = $dir_tmp."tmp_".$numpad.".jpg";
		
		fwrite($fid1, $counter[$i]."\t");
		$photos_for_recognizer[$i]= $photos_me[$k]['src_big'];
		$photos_for_recognizer_x[$i]=$photos_me_tagged[$j]['xcoord'];
		$photos_for_recognizer_y[$i]=$photos_me_tagged[$j]['ycoord'];
		fwrite($fid1, $photos_me[$k]['src_big']."\t");
		fwrite($fid1, $photos_me_tagged[$j]['xcoord']."\t");
		fwrite($fid1, $photos_me_tagged[$j]['ycoord']."\n");
		//fwrite($fid1, $photos_me_tagged[$k]['src_big']."\n");
		
		$exec_string_wget = 'wget '.$photos_me[$k]['src_big'].' -O '.$counter[$i];
		//print_r($exec_string_wget);
		if ($put_scr==true) echo("<br>");
		exec($exec_string_wget, $output);
		//print_r($output);
				
		$i++;
		
		}
	}
}
//print_r($photos_for_recognizer);
$number_photo[2] = $i-1;
}

fclose($fid1);

if ($put_scr==true) echo("<br>Done.");


// compute time durationn
$totaltime_fb_login_retrieve = ($endtime_fb_login_retrieve - $starttime_fb_login_retrieve);



// write time information to file
$file_info_time = $dir_final_log."step1-retrieve_pictures.txt";
$fid_time = fopen($file_info_time, 'w') or die("can't open file");
fwrite($fid_time, $starttime_fb_login_retrieve."\t".$endtime_fb_login_retrieve."\t".$totaltime_fb_login_retrieve."\t");
fclose($fid_time);

$file_info_time_general = $dir_final_log."general.txt";
$fid_time2 = fopen($file_info_time_general, 'w') or die("can't open file");
fwrite($fid_time2, $start_time_today."\t".$start_time_time."\t");
fclose($fid_time2);


//write any output to a file to check 
echo("</body></html>");
$c=ob_get_contents();
ob_end_clean();
$fp=fopen($dir_final_log."01_retrieve_pictures.html", "wb");
fwrite($fp, $c);
fclose($fp);

echo("file=".$myFile1."&dir_check=".$dir_check."&dir_tmp=".$dir_tmp."&prefix_dir=".$prefix_dir."&dir_final=".$dir_final."&dir_video=".$dir_video."&dir_final_log=".$dir_final_log);


?>

